<?php
include "Includes/solotrn_sesion.php";
include "Includes/soloFun_Generales.php";
require_once "config.php";
require_once "Includes/conexion.class.php";

//printArray($_POST);
if ($_REQUEST["HidId"] != "") {
    $tabla = "PROVINCIA";
    switch ($_REQUEST["opc"]) {
        case ("M"):
            $des = strtoupper($_REQUEST["TxtDesc"]);

            $tipo = "UPDATE";
            //Valida si no se ha modificado nada, regresa con OK
            if ($des == $_REQUEST["TxtDesc0"]) {
                header("Location:provincia.php?MiError=0");
            }

            if(preg_match("/^([A-ZñÑáéíóúÁÉÍÓÚ]*\s?)+$/", utf8_encode($des)) > 0){
                $strSql="$tipo $tabla set ";
                $strSql .= "des='" . $_REQUEST["TxtDesc"] . "' ";
                $strSql .=  "where id=" . $_REQUEST["HidId"];
            }
            break;
        case ("E"):
            //Verificar que no tenga cantones asignados antes de eliminar
            $sql = "SELECT COUNT(*) FROM CANTON WHERE id_provincia = " . $_REQUEST["HidId"];
            $oConexion = new Conexion($MIES["motor_base"], $MIES["db_usuario"], $MIES["db_password"],
                    $MIES["basedatos"], $MIES["servidor"], $MIES["flags"], $MIES["encoding"]);

            $conteo = SoloFun_ExecSqlFull($sql, $Arreglo, $oConexion);

            if(!is_null($conteo) && $conteo > 0){
                echo "No se puede eliminar porque contiene cantones";
            }else{
                $tipo = "DELETE";
                $strSql="$tipo from $tabla where id=" . $_REQUEST["HidId"];
            }
            break;
        default:
            $des = strtoupper($_REQUEST["ATxtDesc"]);
            if(preg_match("/^([A-ZñÑáéíóúÁÉÍÓÚ]*\s?)+$/", utf8_encode($des)) > 0){
                $tipo = "INSERT";
                $strSql="$tipo into $tabla (des) values ('$des')";
            }
            break;
    }

    $arrQueries = array();
    array_push($arrQueries, $strSql);
    //array_push($arrQueries, crearQueryLog($tabla, $tipo, $strSql));
    $oConexion = new Conexion($MIES["motor_base"], $MIES["db_usuario"], $MIES["db_password"],
            $MIES["basedatos"], $MIES["servidor"], $MIES["flags"], $MIES["encoding"]);
    SoloFun_ExecSqlFull($strSql, $Arreglo, $oConexion);

    unset($oConexion);
    $oConexion = new Conexion($WORKFLOW["motor_base"], $WORKFLOW["db_usuario"], $WORKFLOW["db_password"],
            $WORKFLOW["basedatos"], $WORKFLOW["servidor"], $WORKFLOW["flags"], $WORKFLOW["encoding"]);

    $log = crearQueryLog($tabla, $tipo, $strSql);
//    echo $log;
    $oConexion->EjecutarQuery($log);

    //$oConexion->EjecutarTransaccion($arrQueries);
    $oConexion->Cerrar();

    if (!$exito) {
//'	 response.write STRsqL & err.description
        $strMsg="provincia.php?MiError=1&des=" . $oConexion->getErrorMessage();
        //echo $strMsg;
    }else {
        $strMsg="provincia.php?MiError=0";
    }
    header ("Location: $strMsg");

}
//'CODIGO CUANDO SE SUBMITE LA PAGINA
switch  ($_REQUEST["opc"]) {
    case ("M"):
        $boton ="<input type='submit' name='CmdGrabar' value='Modificar' class='SoloStyle_Boton'>";
        $oculto="<input type='hidden' name='opc' value='M'>";
        $titulo="Modificaci&oacute;n de Provincia";
        break;
    case ("E"):
        $boton ="<input type='submit' name='CmdGrabar' value='Eliminar' class='SoloStyle_Boton'>";
        $oculto="<input type='hidden' name='opc' value='E'>";
        $titulo="Eliminaci&oacute;n de Provincia";
}
?>

<html>
    <head>
        <title>Documentaci&oacute;n</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <link rel="stylesheet" href="SoloStyles/Solostyles.css" type="text/css">
    </head>

    <body topmargin=0 leftmargin=0>
        <p align="center"class="SoloStyle_TituloPaginas"><?php echo $titulo; ?></p>

        <?php if ($_REQUEST["opc"] != "C") { ?>
        <form name="frmIngreso" action="trn_GrabaProvincia.php" method="POST">
            <table border="0" width="70%" align="center">
                <tr>
                    <td class="SoloStyle_Etiqueta"><b>Id</b></td>
                    <td class="soloStyle_Texto"><?php echo $_REQUEST["id"];?><input type="hidden" name="HidId" value="<?php echo $_REQUEST["id"];?>"></td>
                </tr>
                <tr>
                    <td class="SoloStyle_Etiqueta">Provincia</td>
                    <td><input type="text" maxlength="30" size="50" name="TxtDesc" class="SoloStyle_Texto" value="<?php echo $_REQUEST["ds"];?>">
                        <input type="hidden" name="TxtDesc0" value="<?php $_REQUEST["ds"];?>">
                    </td>
                </tr>
            </table>
            <br><center><?php echo $boton;
    echo $oculto;?></center>
        </form>
        <br>
    <?php } else { ?>
        <table width="75%" border="0" align="center" class="grid">
            <tr class="SoloStyle_Etiqueta">
                <td>Provincia: <?php echo $_REQUEST["ds"];?></td>
            </tr>
    <?php for ($i=0; $i < count($arrData);$i++) {?>
            <tr>
                <td><?php echo $arrData[$i][2];?></td>

            </tr>
        <?php }?>
        </table>
        <br><center><?php echo $boton; ?></center>
    <?php } ?>
    </body>
</html>
